<template>
  <el-dialog v-model="dialogFormVisible" title='申请出库' width="600px" align-center draggable>
    <div class="product-name">您申请出库的产品是:&nbsp;&nbsp;{{ formData.product_name }}</div>
    <div class="product-name">该产品的库存还有:&nbsp;&nbsp;{{ formData.product_in_warehouse_number }}</div>
    <div class="dialog-content">
      <el-form ref="ruleFormRef" :model="formData" :label-position="labelPosition" :rules="rules" label-width="120px">
        <el-form-item label="申请出库编号" prop="product_out_id">
          <el-input v-model="formData.product_out_id" disabled />
        </el-form-item>
        <el-form-item label="出库数量" prop="product_out_number">
          <el-input v-model="formData.product_out_number" />
        </el-form-item>
        <el-form-item label="出库申请人" prop="product_out_apply_person">
          <el-input v-model="formData.product_out_apply_person" disabled />
        </el-form-item>
        <el-form-item label="产品单价" prop="product_single_price">
          <el-input v-model="formData.product_single_price" disabled />
        </el-form-item>
        <el-form-item label="申请出库备注" prop="apply_memo">
          <el-input v-model="formData.apply_memo" :rows="2" type="textarea" />
        </el-form-item>
      </el-form>
    </div>
    <template #footer>
      <span class="dialog-footer">
        <el-button type="primary" @click="applyProduct"
          :disabled='formData.product_in_warehouse_number < Number(formData.product_out_number)'>
          确定
        </el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script lang="ts" setup>
import { reactive, ref, onBeforeUnmount } from 'vue'
import { ApplyProductOutAPI } from '../../../api/product'
import { ElMessage } from 'element-plus'
import { bus } from "../../../utils/mitt.js"
import { tracking } from '../../../utils/operate'

const props = defineProps<{
  getProductList: Function,
}>()


bus.on('applyProductInfo', (row: any) => {
  formData.id = row.id
  formData.product_in_warehouse_number = row.product_in_warehouse_number
  formData.product_name = row.product_name
  formData.product_single_price = row.product_single_price
  formData.product_out_number = ''
  formData.product_out_id = row.product_id
})



const labelPosition = ref('left')

interface FormData {
  id: number | null,
  product_name: string | null,
  product_out_id: number | null | string,
  product_in_warehouse_number: number | null,
  product_single_price: number | null,
  product_out_number: number | null | string,
  product_out_apply_person: string,
  apply_memo: string,
}

const formData: FormData = reactive({
  id: null,
  product_name: null,
  product_out_id: null,
  product_in_warehouse_number: null,
  product_single_price: null,
  product_out_number: null,
  product_out_apply_person: JSON.parse(localStorage.getItem('userinfo') as string).name,
  apply_memo: ''
})

const rules = reactive({
  product_out_id: [
    { required: true, message: '请输入申请出库编号', trigger: 'blur' },
  ],
  product_out_number: [
    { required: true, message: '请输入申请出库数量', trigger: 'blur' },
  ],
  product_out_apply_person: [
    { required: true, message: '请输入申请人', trigger: 'blur' },
  ],
})

// 产品出库
const applyProduct = async () => {
  const res = await ApplyProductOutAPI(formData)
  if (res.data.status == 0) {
    ElMessage({
      message: '产品申请出库成功',
      type: 'success',
    })
    props.getProductList()
    dialogFormVisible.value = false
    tracking("产品管理", "申请出库", { name: formData.product_name, number: formData.product_out_number, unit: '个' }, "中级")
  } else {
    ElMessage.error('产品申请出库失败')
    dialogFormVisible.value = false
  }
}
// 弹窗开关
const dialogFormVisible = ref(false)

// 打开创建管理员的弹窗
const open = () => {
  dialogFormVisible.value = true
}

defineExpose({
  open
})


// 以避免内存泄漏
onBeforeUnmount(() => {
  bus.all.clear()
})
</script>

<style lang="scss" scoped>
.dialog-content {
  display: flex;
  padding: 0 30px 20px 30px;
}

.product-name {
  display: flex;
  justify-content: center;
  font-size: 16px;
  margin: 10px;
  color: #333;
}

:deep(.el-form-item) {
  margin: 30px;
}
</style>